import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/request/token'
// import cookies from 'js-cookie'

// axios.defaults.xsrfHeaderName = 'X-CSRFTOKEN'
// axios.defaults.xsrfCookieName = 'csrftoken'
// axios.defaults.withCredentials = true
axios.defaults.withCredentials = true

const service = axios.create({
  baseURL: process.env.BASE_API,
  timeout: 1000000,
  xsrfHeaderName: 'X-CSRFTOKEN',
  xsrfCookieName: 'csrftoken'
})

// request拦截器
service.interceptors.request.use(config => {
  if (store.state.token) {
    config.headers['Oauth-Token'] = getToken()
  }
  if (config.method === 'post') {
  }
  return config
}, error => {
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    // 全局统一处理 Session超时
    if (response.headers['session_time_out'] === 'timeout') {
      store.dispatch('fedLogOut')
    }
    const res = response.data

    // 0 为成功状态
    if (res.code !== 0) {
      // 90001 Session超时
      if (res.code === 90001) {
        // eslint-disable-next-line
        return Promise.reject('error')
      }

      // 401 用户未登录
      if (res.code === 1005) {
        console.info('用户未登录')

        Message({
          type: 'warning',
          showClose: true,
          message: '未登录或登录超时，请重新登录哦'
        })

        store.dispatch('fedLogOut')
        // eslint-disable-next-line
        return Promise.reject('error')
      }

      // 70001 权限认证错误
      if (res.code === 70001) {
        console.info('权限认证错误')
        Message({
          type: 'warning',
          showClose: true,
          message: '你没有权限访问哦'
        })
        // eslint-disable-next-line
        return Promise.reject('error')
      }
      return Promise.reject(res.msg)
    } else {
      return response.data
    }
  }, error => {
    Message({
      type: 'warning',
      showClose: true,
      message: '连接超时'
    })
    return Promise.reject(error)
  })

export default service
